package com.gzhryc.shared_device.common.device.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.device.dao.DeviceTypeVersionDao;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeVersion;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class DeviceTypeVersionService {

    static Logger log = Logger.getLogger(DeviceTypeVersionService.class);

    public static DeviceTypeVersionService self() {
        return MultiDBTools.getService(DeviceTypeVersionService.class, true);
    }

    public static DeviceTypeVersionService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, DeviceTypeVersionService.class, true);
    }

    DeviceTypeVersionDao dao;

    public DeviceTypeVersionService(String jdbcKey) {
        dao = new DeviceTypeVersionDao(jdbcKey);
    }

    public List<DeviceTypeVersion> findByTypeId(String typeId, Integer pageNo, Integer pageLimit){
        if(StringTools.isNotBlank(typeId)) {
            Conditions conditions = new Conditions(DeviceTypeVersion.class);
            conditions.and().is("typeId",typeId);

            try {
                return dao.findByCondition(conditions,pageNo,pageLimit);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countByTypeId(String typeId){
        if(StringTools.isNotBlank(typeId)) {
            Conditions conditions = new Conditions(DeviceTypeVersion.class);
            conditions.and().is("typeId",typeId);

            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public DeviceTypeVersion getByKeys(String typeId,String versionNumber){
        if(StringTools.isNotBlank(typeId) && StringTools.isNotBlank(versionNumber)){
            Conditions conditions = new Conditions(DeviceTypeVersion.class);
            conditions.and().is("typeId",typeId);
            conditions.and().is("versionNumber",versionNumber);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(DeviceTypeVersion deviceTypeVersion) throws LogicException {
        deviceTypeVersion.setCreateDate(new Date());
        try {
            return dao.insert(deviceTypeVersion) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(DeviceTypeVersion deviceTypeVersion)throws LogicException {
        if(StringTools.isNotBlank(deviceTypeVersion.getTypeId()) && StringTools.isNotBlank(deviceTypeVersion.getVersionNumber())) {
            Conditions conditions = new Conditions(DeviceTypeVersion.class);
            conditions.and().is("typeId", deviceTypeVersion.getTypeId());
            conditions.and().is("versionNumber", deviceTypeVersion.getVersionNumber());

            try {
                return dao.updateNotNull(deviceTypeVersion,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(String typeId,String versionNumber) throws LogicException{
        if(StringTools.isNotBlank(typeId) && StringTools.isNotBlank(versionNumber)){
            Conditions conditions = new Conditions(DeviceTypeVersion.class);
            conditions.and().is("typeId",typeId);
            conditions.and().is("versionNumber",versionNumber);

            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
